home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / WAIS / next-ui / QuestionDoc.m < prev    next >
Encoding:
Text File  |  1992-02-23  |  6.2 KB  |  246 lines

  1. // QuestionDoc.m
  2. //
  3. // Free software created 1 Feb 1992
  4. // by Paul Burchard <burchard@math.utah.edu>.
  5.  
  6. #import "Wais.h"
  7. #import "WAISControl.h"
  8. #import "QuestionDoc.h"
  9. #import "IconWell.h"
  10. #import "IconWellControl.h"
  11. #import "BrowserBarChart.h"
  12. #import "string.h"
  13. #import <appkit/appkit.h>
  14.  
  15.  
  16. @implementation QuestionDoc
  17.  
  18. + (const char *)fileType
  19. {
  20.     return "qst";
  21. }
  22.  
  23. + (const char *)nibName
  24. {
  25.     return "QuestionDoc.nib";
  26. }
  27.  
  28. + (const char *)miniIconName
  29. {
  30.     return "WaisQuestion.tiff";
  31. }
  32.  
  33. + (const char *)defaultFolder
  34. {
  35.     return [[WaisQuestion folderList] elementAt:0];
  36. }
  37.  
  38. - init
  39. {
  40.     [super init];
  41.     waisQuestion = [[WaisQuestion alloc] initKey:NULL];
  42.     iconWellControl = [[IconWellControl alloc] initWindow:window];
  43.     [[[resultBrowser setAbbreviated:YES] setAlphabetized:NO] setEditable:NO];
  44.     [resultWell setDraggable:YES droppable:NO];
  45.     [[[similarBrowser setAbbreviated:YES] setAlphabetized:YES] setEditable:YES];
  46.     [similarWell setDraggable:YES droppable:YES];
  47.     [[[sourceBrowser setAbbreviated:YES] setAlphabetized:YES] setEditable:YES];
  48.     [sourceWell setDraggable:YES droppable:YES];
  49.     [[[keyWordField docView] setDelegate:self] setSelectable:YES];
  50.     [[keyWordField docView] setCharFilter:(NXCharFilterFunc)NXFieldFilter];
  51.     [window makeFirstResponder:[keyWordField docView]];
  52.     [[keyWordField docView] setSel:0 :0];
  53.     return self;
  54. }
  55.  
  56. - free
  57. {
  58.     [waisQuestion free];
  59.     [iconWellControl free];
  60.     return [super free];
  61. }
  62.  
  63. - textDidEnd:sender endChar:(unsigned short)whyEnd
  64. {
  65.     // Search when user hits Return in text window.
  66.     if(whyEnd == NX_RETURN) [searchButton performClick:self];
  67.     
  68.     // Return first responder status back to text object.
  69.     [window makeFirstResponder:[keyWordField docView]];
  70.     [[keyWordField docView] setSel:0 :0];
  71.     return self;
  72. }
  73.  
  74. - updateToWindow
  75. {
  76.     int i, j, n;
  77.     id theList, doc;
  78.     
  79.     // Report keywords.
  80.     if([waisQuestion valueForStringKey:":seed-words"])
  81.         [[keyWordField docView] setText:[waisQuestion 
  82.         valueForStringKey:":seed-words"]];
  83.  
  84.     // Report sources.
  85.     theList = [waisQuestion sourceList];
  86.     n = [theList count];
  87.     [sourceBrowser clear];
  88.     for(i=0; i<n; i++) [sourceBrowser addEntry:[[theList objectAt:i] key]];
  89.     [sourceBrowser update];
  90.  
  91.     // Report relevant docs.
  92.     theList = [waisQuestion relevantList];
  93.     n = [theList count];
  94.     [similarBrowser clear];
  95.     for(i=0; i<n; i++) [similarBrowser addEntry:[[theList objectAt:i] key]];
  96.     [similarBrowser update];
  97.  
  98.     // Report result documents and their scores.
  99.     theList = [waisQuestion resultList];
  100.     n = [theList count];
  101.     [resultBrowser clear];
  102.     for(i=0; i<n; i++)
  103.     {
  104.         doc = [theList objectAt:i];
  105.         j = [resultBrowser indexAddEntry:[doc key]];
  106.     [resultBrowser setBarValue:[waisQuestion scoreForDocument:doc] at:j];
  107.     }
  108.     [resultBrowser update];
  109.     return self;
  110. }
  111.  
  112. - updateFromWindow
  113. {
  114.     int i;
  115.     const char *dockey, *srckey;
  116.     char buf[STRINGSIZE+1];
  117.     id doc, source, stringTable;
  118.  
  119.     // Update keywords.
  120.     [window setDocEdited:YES];
  121.     [[keyWordField docView] getSubstring:buf start:0 length:STRINGSIZE];
  122.     [waisQuestion setKeywords:buf];
  123.     
  124.     // Update relevant document list.
  125.     // Load in any unknown new docs.
  126.     // Ignore doc if it is still being retrieved (to avoid thread collisions).
  127.     [waisQuestion clearRelevantDocuments];
  128.     for(i=0; dockey=[similarBrowser entryAt:i]; i++)
  129.     {
  130.         if(!(doc = [WaisDocument objectForKey:dockey]))
  131.     {
  132.         doc = [[WaisDocument alloc] initKey:dockey];
  133.         if(![doc readWaisFile]) 
  134.         {
  135.             [doc free];
  136.         stringTable = [[NXApp delegate] stringTable];
  137.         if(stringTable) NXRunAlertPanel(
  138.             [stringTable valueForStringKey:"WAIS Question Error!"],
  139.             [stringTable valueForStringKey:
  140.             "Can't find WAIS specifications for document %s"],
  141.             [stringTable valueForStringKey:"OK"], NULL, NULL, dockey);
  142.         continue;
  143.         }
  144.     }
  145.     if(![[NXApp delegate] isDocumentBeingRetrieved:doc])
  146.         [waisQuestion addRelevantDocument:doc];
  147.     }
  148.     
  149.     // Update source list.
  150.     // Load in any unknown new sources.
  151.     [waisQuestion clearSources];
  152.     for(i=0; srckey=[sourceBrowser entryAt:i]; i++)
  153.     {
  154.         if(!(source = [WaisSource objectForKey:srckey]))
  155.     {
  156.         source = [[WaisSource alloc] initKey:srckey];
  157.         if(![source readWaisFile])
  158.         {
  159.             [source free];
  160.         stringTable = [[NXApp delegate] stringTable];
  161.         if(stringTable) NXRunAlertPanel(
  162.             [stringTable valueForStringKey:"WAIS Question Error!"],
  163.             [stringTable valueForStringKey:
  164.             "Can't find WAIS specifications for source %s"],
  165.             [stringTable valueForStringKey:"OK"], NULL, NULL, srckey);
  166.         continue;
  167.         }
  168.     }
  169.     [waisQuestion addSource:source];
  170.     }
  171.     return self;
  172. }
  173.  
  174. - dump:sender
  175. {
  176.     if(!fileName) return nil;
  177.     [self updateFromWindow];
  178.     if(![waisQuestion writeWaisFile]) return nil;
  179.     return self;
  180. }
  181.  
  182. - load:sender
  183. {
  184.     if(!fileName) return nil;
  185.     if(![waisQuestion readWaisFile]) return nil;
  186.     [self updateToWindow];
  187.     return self;
  188. }
  189.  
  190. - setFileName:(const char *)aName
  191. {
  192.     id rtn = [super setFileName:aName];
  193.     if(rtn) [waisQuestion setKey:fileName];
  194.     return(rtn);
  195. }
  196.  
  197. - search:sender
  198. {
  199.     [self updateFromWindow];
  200.     if(![waisQuestion search]) return nil;
  201.     [self updateToWindow];
  202.     return self;
  203. }
  204.  
  205. - (int)prepFile:(const char *)fileKey ok:(int *)flag
  206. {
  207.     // Begin retrieval of documents as they are dragged out.
  208.     *flag = NO;
  209.     if(![[NXApp delegate] retrieveDocuments:fileKey]) return 0;
  210.     *flag = YES;
  211.     return 0;
  212. }
  213.  
  214. - (int)openFile:(const char *)fileKey ok:(int *)flag
  215. {
  216.     id rtn, doc, source;
  217.     
  218.     // If known source, open in this application.
  219.     if(source = [WaisSource objectForKey:fileKey])
  220.     return [[NXApp delegate] openFile:fileKey ok:flag];
  221.     
  222.     // If retrieved document, open in this application (and so in Workspace).
  223.     // If unretrieved document, call WAIS to retrieve.
  224.     if(doc = [WaisDocument objectForKey:fileKey])
  225.     {
  226.         if([[NXApp delegate] isDocumentBeingRetrieved:doc])
  227.         return 0;
  228.         if([doc isRetrieved])
  229.         return [[NXApp delegate] openFile:fileKey ok:flag];
  230.     else
  231.     {
  232.         rtn = [[NXApp delegate] retrieveDocuments:[doc key]];
  233.         if(rtn) *flag = YES;
  234.         else *flag = NO;
  235.         return 0;
  236.     }
  237.     }
  238.     
  239.     // Unknown file key---ignore.
  240.     return 0;
  241. }
  242.  
  243. @end
  244.  
  245.  
  246.